<?php
/*
 * (C) Copyright 2013 Torsten Gorath (http://www.blauesdach.de/).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Torsten Gorath
 */
require_once $basedir."/inc/bankverbindung.php";
$bank = new Bankverbindung();
if(isset($_REQUEST['id'])) {
	$bank = new Bankverbindung($_REQUEST['id']);
}
if(isset($_REQUEST['save'])) {
	$bank->setBlz($_REQUEST['blz']);
	$bank->setEinzugsermaechtigung($_REQUEST['einzugsermaechtigung']=="checked");
	$bank->setKontonummer($_REQUEST['kontonummer']);
	$bank->setName($_REQUEST['name']);
	$bank->save();
}
if($action=='edit') {
	$action = 'add';
}
if($action=='add') {
?>
<form id='stammdaten_bank_add_form' method="get" action="/index.php">
	<input type="hidden" name="page" value="<?php echo $menuid;?>" />
	<input type="hidden" name="id" value="<?php echo $bank->getId();?>" />
    <div>
        <label>Kontoinhaber:</label>
        <input type="text" name="name" maxlength="100" value="<?php echo $bank->getName();?>" />
    </div>
    <div>
        <label>BLZ:</label>
        <input type="number" name="blz" maxlength="10" value="<?php echo $bank->getBlz();?>" />
    </div>
    <div>
        <label>Kontonummer:</label>
        <input type="number" name="kontonummer" maxlength="10" value="<?php echo $bank->getKontonummer();?>" />
    </div>
    <div>
        <label>Einzugserm&auml;chtigung:</label>
        <input type="checkbox" name="einzugsermaechtigung" <?php echo $bank->isEinzugsermaechtigung()?" checked":"";?> value="checked"/>
    </div>
    <div>
        <button type="submit" name="save">Speichern</button>
        <button type="submit" name="reset">Abbrechen</button>
    </div>
</form>
<?php
} else {
	$banken = Bankverbindung::getAll();
?>
<div>
    <a href='?page=stammdaten_bank&action=add'>Neue Bankverbindung</a>
</div>
<div>
    <table border="1" bordercolor="black" cellpadding="0" cellspacing="0" >
        <tr>
            <th>Kontoinhaben</th>
            <th>BLZ</th>
            <th>Kontonummer</th>
            <th>Einzugserm&auml;chtigung</th>
            <th></th>
        </tr>
<?php
if($banken!=NULL) {
	for($i=0;$i<count($banken);$i++) {
		$value = $banken[$i];
?>
		<tr>
			<td><?php echo $value->getName();?></td>
			<td><?php echo $value->getBlz();?></td>
			<td><?php echo $value->getKontonummer();?></td>
			<td><?php echo $value->isEinzugsermaechtigung();?></td>
			<td><a href="?page=<?php echo $menuid; ?>&action=edit&id=<?php echo $value->getId();?>">edit</a></td>
		</tr>
<?php
	}
}
?>
    </table>
</div>
<?php    
}
?>